﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;
    using System.Collections;

    internal class Code39 : BarcodeCommon, IBarcode
    {
        private bool _AllowExtended;
        private Hashtable C39_Code;
        private Hashtable ExtC39_Translation;

        public Code39(string input)
        {
            this.C39_Code = new Hashtable();
            this.ExtC39_Translation = new Hashtable();
            this._AllowExtended = false;
            base.Raw_Data = input;
        }

        public Code39(string input, bool AllowExtended)
        {
            this.C39_Code = new Hashtable();
            this.ExtC39_Translation = new Hashtable();
            this._AllowExtended = false;
            base.Raw_Data = input;
            this._AllowExtended = AllowExtended;
        }

        private string Encode_Code39()
        {
            this.init_Code39();
            this.init_ExtendedCode39();
            string formattedData = "*" + base.Raw_Data.Replace("*", "") + "*";
            if (this._AllowExtended)
            {
                this.InsertExtendedCharsIfNeeded(ref formattedData);
            }
            base.FormattedData = formattedData;
            string str2 = "";
            foreach (char ch in base.FormattedData)
            {
                try
                {
                    str2 = str2 + this.C39_Code[ch].ToString() + "0";
                }
                catch
                {
                    if (this._AllowExtended)
                    {
                        throw new Exception("EC39-1: Invalid data.");
                    }
                    throw new Exception("EC39-1: Invalid data. (Try using Extended Code39)");
                }
            }
            str2 = str2.Substring(0, str2.Length - 1);
            this.C39_Code.Clear();
            return str2;
        }

        private void init_Code39()
        {
            this.C39_Code.Clear();
            this.C39_Code.Add('0', "101001101101");
            this.C39_Code.Add('1', "110100101011");
            this.C39_Code.Add('2', "101100101011");
            this.C39_Code.Add('3', "110110010101");
            this.C39_Code.Add('4', "101001101011");
            this.C39_Code.Add('5', "110100110101");
            this.C39_Code.Add('6', "101100110101");
            this.C39_Code.Add('7', "101001011011");
            this.C39_Code.Add('8', "110100101101");
            this.C39_Code.Add('9', "101100101101");
            this.C39_Code.Add('A', "110101001011");
            this.C39_Code.Add('B', "101101001011");
            this.C39_Code.Add('C', "110110100101");
            this.C39_Code.Add('D', "101011001011");
            this.C39_Code.Add('E', "110101100101");
            this.C39_Code.Add('F', "101101100101");
            this.C39_Code.Add('G', "101010011011");
            this.C39_Code.Add('H', "110101001101");
            this.C39_Code.Add('I', "101101001101");
            this.C39_Code.Add('J', "101011001101");
            this.C39_Code.Add('K', "110101010011");
            this.C39_Code.Add('L', "101101010011");
            this.C39_Code.Add('M', "110110101001");
            this.C39_Code.Add('N', "101011010011");
            this.C39_Code.Add('O', "110101101001");
            this.C39_Code.Add('P', "101101101001");
            this.C39_Code.Add('Q', "101010110011");
            this.C39_Code.Add('R', "110101011001");
            this.C39_Code.Add('S', "101101011001");
            this.C39_Code.Add('T', "101011011001");
            this.C39_Code.Add('U', "110010101011");
            this.C39_Code.Add('V', "100110101011");
            this.C39_Code.Add('W', "110011010101");
            this.C39_Code.Add('X', "100101101011");
            this.C39_Code.Add('Y', "110010110101");
            this.C39_Code.Add('Z', "100110110101");
            this.C39_Code.Add('-', "100101011011");
            this.C39_Code.Add('.', "110010101101");
            this.C39_Code.Add(' ', "100110101101");
            this.C39_Code.Add('$', "100100100101");
            this.C39_Code.Add('/', "100100101001");
            this.C39_Code.Add('+', "100101001001");
            this.C39_Code.Add('%', "101001001001");
            this.C39_Code.Add('*', "100101101101");
        }

        private void init_ExtendedCode39()
        {
            this.ExtC39_Translation.Clear();
            this.ExtC39_Translation.Add(Convert.ToChar(0).ToString(), "%U");
            this.ExtC39_Translation.Add(Convert.ToChar(1).ToString(), "$A");
            this.ExtC39_Translation.Add(Convert.ToChar(2).ToString(), "$B");
            this.ExtC39_Translation.Add(Convert.ToChar(3).ToString(), "$C");
            this.ExtC39_Translation.Add(Convert.ToChar(4).ToString(), "$D");
            this.ExtC39_Translation.Add(Convert.ToChar(5).ToString(), "$E");
            this.ExtC39_Translation.Add(Convert.ToChar(6).ToString(), "$F");
            this.ExtC39_Translation.Add(Convert.ToChar(7).ToString(), "$G");
            this.ExtC39_Translation.Add(Convert.ToChar(8).ToString(), "$H");
            this.ExtC39_Translation.Add(Convert.ToChar(9).ToString(), "$I");
            this.ExtC39_Translation.Add(Convert.ToChar(10).ToString(), "$J");
            this.ExtC39_Translation.Add(Convert.ToChar(11).ToString(), "$K");
            this.ExtC39_Translation.Add(Convert.ToChar(12).ToString(), "$L");
            this.ExtC39_Translation.Add(Convert.ToChar(13).ToString(), "$M");
            this.ExtC39_Translation.Add(Convert.ToChar(14).ToString(), "$N");
            this.ExtC39_Translation.Add(Convert.ToChar(15).ToString(), "$O");
            this.ExtC39_Translation.Add(Convert.ToChar(0x10).ToString(), "$P");
            this.ExtC39_Translation.Add(Convert.ToChar(0x11).ToString(), "$Q");
            this.ExtC39_Translation.Add(Convert.ToChar(0x12).ToString(), "$R");
            this.ExtC39_Translation.Add(Convert.ToChar(0x13).ToString(), "$S");
            this.ExtC39_Translation.Add(Convert.ToChar(20).ToString(), "$T");
            this.ExtC39_Translation.Add(Convert.ToChar(0x15).ToString(), "$U");
            this.ExtC39_Translation.Add(Convert.ToChar(0x16).ToString(), "$V");
            this.ExtC39_Translation.Add(Convert.ToChar(0x17).ToString(), "$W");
            this.ExtC39_Translation.Add(Convert.ToChar(0x18).ToString(), "$X");
            this.ExtC39_Translation.Add(Convert.ToChar(0x19).ToString(), "$Y");
            this.ExtC39_Translation.Add(Convert.ToChar(0x1a).ToString(), "$Z");
            this.ExtC39_Translation.Add(Convert.ToChar(0x1b).ToString(), "%A");
            this.ExtC39_Translation.Add(Convert.ToChar(0x1c).ToString(), "%B");
            this.ExtC39_Translation.Add(Convert.ToChar(0x1d).ToString(), "%C");
            this.ExtC39_Translation.Add(Convert.ToChar(30).ToString(), "%D");
            this.ExtC39_Translation.Add(Convert.ToChar(0x1f).ToString(), "%E");
            this.ExtC39_Translation.Add("!", "/A");
            this.ExtC39_Translation.Add("\"", "/B");
            this.ExtC39_Translation.Add("#", "/C");
            this.ExtC39_Translation.Add("$", "/D");
            this.ExtC39_Translation.Add("%", "/E");
            this.ExtC39_Translation.Add("&", "/F");
            this.ExtC39_Translation.Add("'", "/G");
            this.ExtC39_Translation.Add("(", "/H");
            this.ExtC39_Translation.Add(")", "/I");
            this.ExtC39_Translation.Add("*", "/J");
            this.ExtC39_Translation.Add("+", "/K");
            this.ExtC39_Translation.Add(",", "/L");
            this.ExtC39_Translation.Add("/", "/O");
            this.ExtC39_Translation.Add(":", "/Z");
            this.ExtC39_Translation.Add(";", "%F");
            this.ExtC39_Translation.Add("<", "%G");
            this.ExtC39_Translation.Add("=", "%H");
            this.ExtC39_Translation.Add(">", "%I");
            this.ExtC39_Translation.Add("?", "%J");
            this.ExtC39_Translation.Add("[", "%K");
            this.ExtC39_Translation.Add(@"\", "%L");
            this.ExtC39_Translation.Add("]", "%M");
            this.ExtC39_Translation.Add("^", "%N");
            this.ExtC39_Translation.Add("_", "%O");
            this.ExtC39_Translation.Add("{", "%P");
            this.ExtC39_Translation.Add("|", "%Q");
            this.ExtC39_Translation.Add("}", "%R");
            this.ExtC39_Translation.Add("~", "%S");
            this.ExtC39_Translation.Add("`", "%W");
            this.ExtC39_Translation.Add("@", "%V");
            this.ExtC39_Translation.Add("a", "+A");
            this.ExtC39_Translation.Add("b", "+B");
            this.ExtC39_Translation.Add("c", "+C");
            this.ExtC39_Translation.Add("d", "+D");
            this.ExtC39_Translation.Add("e", "+E");
            this.ExtC39_Translation.Add("f", "+F");
            this.ExtC39_Translation.Add("g", "+G");
            this.ExtC39_Translation.Add("h", "+H");
            this.ExtC39_Translation.Add("i", "+I");
            this.ExtC39_Translation.Add("j", "+J");
            this.ExtC39_Translation.Add("k", "+K");
            this.ExtC39_Translation.Add("l", "+L");
            this.ExtC39_Translation.Add("m", "+M");
            this.ExtC39_Translation.Add("n", "+N");
            this.ExtC39_Translation.Add("o", "+O");
            this.ExtC39_Translation.Add("p", "+P");
            this.ExtC39_Translation.Add("q", "+Q");
            this.ExtC39_Translation.Add("r", "+R");
            this.ExtC39_Translation.Add("s", "+S");
            this.ExtC39_Translation.Add("t", "+T");
            this.ExtC39_Translation.Add("u", "+U");
            this.ExtC39_Translation.Add("v", "+V");
            this.ExtC39_Translation.Add("w", "+W");
            this.ExtC39_Translation.Add("x", "+X");
            this.ExtC39_Translation.Add("y", "+Y");
            this.ExtC39_Translation.Add("z", "+Z");
            this.ExtC39_Translation.Add(Convert.ToChar(0x7f).ToString(), "%T");
        }

        private void InsertExtendedCharsIfNeeded(ref string FormattedData)
        {
            string str = "";
            foreach (char ch in base.Raw_Data)
            {
                try
                {
                    string str2 = this.C39_Code[ch].ToString();
                    str = str + ch;
                }
                catch
                {
                    str = str + this.ExtC39_Translation[ch.ToString()].ToString();
                }
            }
            FormattedData = str;
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_Code39();
            }
        }
    }
}

